package com.kova.api.utils;

import java.util.Random;
import java.util.UUID;
import java.util.regex.Pattern;

public abstract class StringUtil {

	private static final Random RND = new Random(UUID.randomUUID().hashCode());
	private static final char[] chars = new char[] {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
	private static final char[] letters = new char[] {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
	private static final char[] charsAll = new char[] {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','!','@','#','$','%','^','&','*'};
	
    /**
     * 返回字符串的值，如果字符串为空则返回默认值
     * 
     * @param src 字符串
     * @param defaultValue 默认值
     * @return
     */
    public static final String nvl(String src, String defaultValue) {
        if (src != null && src.length() > 0) {
            return src;
        } else {
            return defaultValue;
        }
    }

    public static final String nvl(String src) {
        return nvl(src, "");
    }

    /**
     * 判断给入的字符串是否为空,null、""、" "都表示空字符串
     * @param str 待判定的字符串
     * @return 空符串返回true，否则返回false
     */
    public static boolean isEmpty(String str) {
        if (null == str || "".equals(str.trim())) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
    
    /**
     * 判断给入的字符串是否非空
     * @param str
     * @return
     */
    public static boolean isNotEmpty(String str){
    	return !isEmpty(str);
    }
    
    /**
	 * 生成len长度的随机数序列token
	 * 
	 * @param len 长度
	 * @return String 随机数字符
	 */
	public static String buildRandomToken(int len) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < len; i++) {
			sb.append(charsAll[RND.nextInt(charsAll.length)]);
		}
		return sb.toString();
	}

	/**
	 * 生成len长度的随机字母序列
	 *
	 * @param len 长度
	 * @return String 随机字母符
	 */
	public static String buildRandomCodeLetter(int len) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < len; i++) {
			sb.append(letters[RND.nextInt(letters.length)]);
		}
		return sb.toString();
	}
	
	/**
	 * 生成len长度的随机数序列
	 * 
	 * @param len 长度
	 * @return String 随机数字符
	 */
	public static String buildRandomCode(int len) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < len; i++) {
			sb.append(chars[RND.nextInt(chars.length)]);
		}
		return sb.toString();
	}
	
	/**
     * 检查指定的字符串列表是否不为空。
     */
	public static boolean areNotEmpty(String... values) {
		boolean result = true;
		if (values == null || values.length == 0) {
			result = false;
		} else {
			for (String value : values) {
				result &= !isEmpty(value);
			}
		}
		return result;
	}
	
	 /*
	  * 判断是否为正整数 
	  * @param str 传入的字符串 
	  * @return 是整数返回true,否则返回false 
	 */
	  public static boolean isInteger(String str) {
         Pattern pattern = Pattern.compile("^[\\d]+$");
         return pattern.matcher(str).matches();  
	  }

	/**
	 * 使用java正则表达式去掉多余的.与0
	 * @param s
	 * @return  string
	 */
	public static String formatMoney(String s){
		if(null != s && s.indexOf(".") > 0){
			s = s.replaceAll("0+?$", "");//去掉多余的0
			s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
		}
		return s;
	}

	public static String floatFormatString(float f){

		//DecimalFormat decimalFormat=new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
		//String p = decimalFormat.format(f);//format 返回的是字符串
		String p = String.format("%.2f", f);
		return formatMoney(p);
	}

}
